# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================
@Time : 2022/7/12 0012 21:06
@Author : 测试开发工程师
@File : log_utils.py
@Software: PyCharm
============================
"""

import datetime
import logging
import os
from apitest.Test_frame.utils.utils_yaml_path import Utils

# 实例化 logger 对象
logger = logging.getLogger(__name__)

# 定义日志文件路径
log_path = os.sep.join([Utils.get_frame_root_path(), "logs"])

if not os.path.exists(log_path):
    os.mkdir(log_path)

# 绑定 log 的 handler
now = datetime.datetime.now()
c_time = now.strftime('%Y%m%d_%H%M%S')
logfile = c_time + '.log'
file_handler = logging.FileHandler(filename=f"{log_path}/{logfile}", encoding="utf-8")

# 输出的 formatter
formatter = logging.Formatter(
    '[%(asctime).19s] %(process)d:%(levelname).1s %(filename)s:%(lineno)d:%(funcName)s: %(message)s]')

# 日志格式与句柄的绑定
file_handler.setFormatter(formatter)

# 控制台句柄定义
steam_handler = logging.StreamHandler()

# 日志格式与句柄的绑定
steam_handler.setFormatter(formatter)

# 与 logger 进行绑定
logger.addHandler(file_handler)
logger.addHandler(steam_handler)

# 设置展示/写入文件的日志级别
logger.setLevel(logging.INFO)
